package com.hanthink.system.service.impl.repair;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.hanthink.system.domain.repair.RequestInfo;

public class SendWeChatMessage {

    static AccessToken token = null;

    static String tokenTime = null;
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 发送消息的类型
    private final static String MSGTYPE = "markdown";
    // 将消息发送给所有成员
    private final static String TOPARTY = "@all";

    // 获取企业微信的企业号，根据不同企业更改
    private final static String CORPID = "ww2b5be9149ef1ad2b";
    // 获取企业应用的密钥，根据不同应用更改
    private final static String CORPSECRET = "qW82f-gPl3RSnXTvJXAmE_ws4uMZFUFlpwWjnsuffuQ";

    // 測試
//	private final static String CORPID = "wwceec3e6743ebc590";
    // 測試
//	private final static String CORPSECRET = "3mW1XSxsy_Pqn0_LFsMbaUR8yqgMr4utPsP2tHfD8SA";

    // 获取访问权限码URL
    private final static String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
    // 创建会话请求URL
    private final static String CREATE_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";

    private final static String SchURL = "https://qyapi.weixin.qq.com/cgi-bin/oa/schedule/add?access_token=";
    // 太仓企业微信机器人url
    private static String robotUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=b050fd91-14c7-4a5c-8ac7-f81b84b7b9d5";


    // 測試
//		private final static String robotUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f9a16bb9-c0c4-4a96-b9d1-9cd0bdb14cc2";

    // 获取access_token
    public static AccessToken getAccessToken() {

        try {
            boolean createFlag = false;
            if (tokenTime == null) {
                createFlag = true;
            } else {
                Date shiftTokenTime = sdf.parse(tokenTime);
                Calendar oldTime = Calendar.getInstance();
                oldTime.setTime(shiftTokenTime);
                oldTime.add(Calendar.HOUR, 1);
                Date currentTime = new Date();
                createFlag = oldTime.getTime().before(currentTime);
            }
            if (createFlag) {
                tokenTime = sdf.format(new Date());
            } else {
                return token;
            }
            token = new AccessToken();

            // 访问微信服务器
            String url = ACCESS_TOKEN_URL + "?corpid=" + CORPID + "&corpsecret=" + CORPSECRET;

            URL getUrl = new URL(url);
            // 开启连接，并返回一个URLConnection对象
            HttpURLConnection http = (HttpURLConnection) getUrl.openConnection();
            http.setRequestMethod("GET");
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            // 将URL连接用于输入和输出，一般输入默认为true，输出默认为false
            http.setDoOutput(true);
            http.setDoInput(true);
            // 进行连接，不返回对象
            http.connect();

            // 获得输入内容,并将其存储在缓存区
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            // 将内容转化为JSON代码
            String message = new String(buffer, StandardCharsets.UTF_8);
            JSONObject json = JSONObject.parseObject(message);
            // 提取内容，放入对象
            token.setAccess_token(json.getString("access_token"));
            token.setExpires_in(new Integer(json.getString("expires_in")));

        } catch (Exception e) {
            e.printStackTrace();
        }

        // 返回access_token码
        return token;
    }

    /**
     * 企业接口向下属关注用户发送给微信消息
     *
     * @param toUser  成员ID列表
     * @param toParty 部门ID列表
     * @param toTag   标签ID列表
     * @param msg     消息内容
     * @param safe    是否保密
     * @return
     */

    public static void sendWeChatMessagesj(String toUser, String toParty, String toTag, String msg, String safe,
                                         String newsType) {

        if (StringUtil.isEmpty(msg)) {
            return;
        }

        //rf= requestMapper.selectRequestVoById(Long.valueOf(64281));

        String content = "<font color=\\\"warning\\\">工作任务管理系统-"+msg+"提醒</font>\\n";
        //RequestInfo rf =new RequestInfo();
        String[] msgs = { "生产部有抢修单需要审核" };
        for (String mg : msgs) {
            content += ">" + mg + "\\n";
        }

        // 从对象中提取凭证
        AccessToken accessToken = getAccessToken();
        String ACCESS_TOKEN = accessToken.getAccess_token();
        String url = CREATE_SESSION_URL + ACCESS_TOKEN;
        // 请求串
        // 封装发送消息请求JSON
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{");
        stringBuffer.append("\"touser\":" + "\"" + toUser + "\",");
//        stringBuffer.append("\"toparty\":" + "\"" + toParty + "\",");
//        stringBuffer.append("\"totag\":" + "\"" + toTag + "\",");
        stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
        stringBuffer.append("\"markdown\":" + "{");
        stringBuffer.append("\"content\":" + "\"" + content + "\"");
        stringBuffer.append("}");
        stringBuffer.append(",\"safe\":" + "\"" + safe + "\",");
        stringBuffer.append("\"agentid\":" + "\"" + "1000126" + "\",");
//		stringBuffer.append("\"agentid\":" + "\"" + "1000002" + "\",");
        stringBuffer.append("\"debug\":" + "\"" + "1" + "\"");
        stringBuffer.append("}");
        String json = stringBuffer.toString();
        System.out.println(json);

        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void sendWeChatMessage(String toUser, String toParty, String toTag, String msg, String safe,
                                         String newsType) {

        if (StringUtil.isEmpty(msg)) {
            return;
        }
        String content = "<font color=\\\"warning\\\">工作任务管理系统-" + newsType + "提醒</font>\\n";
        String[] msgs = msg.split("\n");
        for (String mg : msgs) {
            content += ">" + mg + "\\n";
        }

        // 从对象中提取凭证
        AccessToken accessToken = getAccessToken();
        String ACCESS_TOKEN = accessToken.getAccess_token();
        String url = CREATE_SESSION_URL + ACCESS_TOKEN;
        // 请求串
        // 封装发送消息请求JSON
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{");
        stringBuffer.append("\"touser\":" + "\"" + toUser + "\",");
//        stringBuffer.append("\"toparty\":" + "\"" + toParty + "\",");
//        stringBuffer.append("\"totag\":" + "\"" + toTag + "\",");
        stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
        stringBuffer.append("\"markdown\":" + "{");
        stringBuffer.append("\"content\":" + "\"" + content + "\"");
        stringBuffer.append("}");
        stringBuffer.append(",\"safe\":" + "\"" + safe + "\",");
        stringBuffer.append("\"agentid\":" + "\"" + "1000126" + "\",");
//		stringBuffer.append("\"agentid\":" + "\"" + "1000002" + "\",");
        stringBuffer.append("\"debug\":" + "\"" + "1" + "\"");
        stringBuffer.append("}");
        String json = stringBuffer.toString();
        System.out.println(json);

        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 企业接口向下属关注用户发送给微信消息
     *
     * @param msg      标签ID列表
     * @param newsType 消息内容
     * @param url      是否保密
     * @return
     */
    public  static void sendWeChatMessageToAll(String msg, String newsType, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "<font color=\\\"warning\\\">工作任务管理系统-" + newsType + "提醒</font>\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 塑胶JIT送货工作任务管理信息推送
     * @param msg
     * @param newsType
     * @param url
     */
    public static void JITsendWeChatMessageToAll(String msg, String newsType, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务<font color=\\\"warning\\\">" + newsType + "</font>提醒\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS1(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务[集货超时](http://121.40.196.244:70/)提醒\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS2(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务<font color=\\\"warning\\\">" + "车辆到厂超时" + "</font>提醒\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS3(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务`车辆出发超时`提醒\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS1delay(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务[集货延误处理](http://121.40.196.244:70/)\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS2delay(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务<font color=\\\"warning\\\">" + "车辆到厂延误处理" + "</font>\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS3delay(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务`车辆出发延误处理`\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS1delaysuper(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务[集货延误回复](http://121.40.196.244:70/)\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS2delaysuper(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务<font color=\\\"warning\\\">" + "车辆到厂延误回复" + "</font>\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void JITsendWeChatMessageToS3delaysuper(String msg, String url) {
        try {
            if (StringUtil.isEmpty(msg)) {
                return;
            }
            String content = "JIT送货工作任务`车辆出发延误回复`\\n";
            String[] msgs = msg.split(",");
            for (String mg : msgs) {
                content += ">" + mg + "\\n";
            }
            // 封装发送消息请求JSON
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("{");
            stringBuffer.append("\"msgtype\":" + "\"" + MSGTYPE + "\",");
            stringBuffer.append("\"markdown\":" + "{");
            stringBuffer.append("\"content\":" + "\"" + content + "\"");
            stringBuffer.append("}");
            stringBuffer.append("}");
            String json = stringBuffer.toString();
            System.out.println(json);

            if (url != null) {
                robotUrl = url;
            }
            URL postUrl = new URL(robotUrl);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();
            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 企业微信新增日程接口
     * 2022-5-19 lizhao develop
     *
     * @param organizer     组织人
     * @param attendeesList 参与者人员列表
     * @param start_time    开始时间
     * @param end_time      结束时间
     * @param summary       会议主题
     * @param description   会议内容
     */
    public void sendWechattoschedule(String organizer, List<String> attendeesList, Date start_time, Date end_time, String summary, String description) {

        long unixTime1 = start_time.getTime() / 1000L;
        long unixTime2 = end_time.getTime() / 1000L;
        // 从对象中提取凭证
        AccessToken accessToken = getAccessToken();
        String ACCESS_TOKEN = accessToken.getAccess_token();
        String url = SchURL + ACCESS_TOKEN;
        // 请求串
        // 封装发送消息请求JSON
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{");
        stringBuffer.append("\"schedule\": {");
        stringBuffer.append("\"organizer\": \"" + organizer + "\",");
        stringBuffer.append("\"start_time\": " + unixTime1 + ",");
        stringBuffer.append("\"end_time\": " + unixTime2 + ",");
        stringBuffer.append("\"attendees\": [");
        for (int i = 0; i < attendeesList.size(); i++) {
            stringBuffer.append("{\"userid\": \"" + attendeesList.get(i) + "\"}");
            if (i != attendeesList.size() - 1) {
                stringBuffer.append(",");
            }
        }
        stringBuffer.append("],\"summary\": \"" + summary + "\",");
        stringBuffer.append("\"description\": \"" + description + "\",");
        stringBuffer.append("\"reminders\": {");
        stringBuffer.append("\"is_remind\": 1,");
        stringBuffer.append("\"remind_before_event_secs\": 900,");
        stringBuffer.append("\"timezone\" : +8");
        stringBuffer.append("}");
        stringBuffer.append("},\"agentid\": 1000126");
        stringBuffer.append("}");
        String json = stringBuffer.toString();
        System.out.println(json);

        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            // 写入内容
            OutputStream outputStream = http.getOutputStream();
            outputStream.write(json.getBytes(StandardCharsets.UTF_8));
            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            String result = new String(jsonBytes, StandardCharsets.UTF_8);
            System.out.println("请求返回结果:" + result);

            // 清空输出流
            outputStream.flush();
            // 关闭输出通道
            outputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
